package net.yxsoft.controller.pc.sysmgt;

import com.jfinal.kit.StrKit;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.model.Area;
import net.yxsoft.service.AreaService;
import rapid.jfinal.Pagination;

/**
 * @author zhaohaibo
 */
public class AreaController extends AuthController {

  private static AreaService srv = AreaService.INSTANCE;

  public void index() {
    render("index.html");
  }


  public void findByPcode() {
    String areaName = "";
    String isSearch = getPara("isSearch");
    if("1".equals(isSearch)){
      areaName = getPara("areaName");
    }
    Pagination rp = Pagination.by(this);

    if(rp.isEnabled()) {
      if(StrKit.notBlank(areaName)){
        rp.bind(srv.pagingByPcode(null, areaName, rp.getPageIndex(), rp.getPageSize()));
      }else {
        rp.bind(srv.pagingByPcode(getPara("pcode", "0"), areaName, rp.getPageIndex(), rp.getPageSize()));
      }
      renderJson(rp);

    } else {
      renderJson(srv.findByPcode(getPara("pcode", "0"),areaName, getPara("excludeCode", null)));
    }
  }


  public void findByRank() {

      renderJson(srv.findByRank(getPara("attrib_05", "1")));

  }


  public void findById() {
    String id = getPara("id");

    if(id == null) {
      renderEmpty();
      return;
    }

    renderJson(srv.findById(id));
  }


  public void save() {
    Area obj = getModel(Area.class, "", true);
    if(obj!=null) {
      if(obj.getConflictId() == null) {
        renderJson(srv.add(obj));
      }else {
        renderJson(srv.update(obj));
      }
    }else {
      renderJson(fail());
    }
  }

  public void delete() {
    String ids = getPara("ids");
    if(StrKit.notBlank(ids)) {
      String[] keys = ids.split(",");
      for(String key:keys) {
        srv.delete(key);
      }

    }
    renderEmpty();
  }
}
